/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.engine;

import com.android1.amarena2d.commons.BlendFunction;
import com.badlogic.gdx.graphics.Color;

public class AmarenaRenderer extends EngineObject implements ContextManager.Listener{

    private AmarenaCamera camera;
    private Director director;
    private final Color clearColor = new Color(Color.BLACK);
    int camWidth = 0;
    int camHeight = 0;
    int surfaceWidth;
    int surfaceHeight;


    public AmarenaRenderer() {
        this.camera = new AmarenaCamera();
    }

    public void surfaceCreated() {

        surfaceWidth = engine.getGraphics().getWidth();
        surfaceHeight = engine.getGraphics().getHeight();

        camWidth = engine.getConfig().getCameraWith();
        if (camWidth == 0)
            camWidth = engine.getGraphics().getWidth();
        camHeight = engine.getConfig().getCameraHeight();
        if (camHeight == 0)
            camHeight = engine.getGraphics().getHeight();

        camera.setViewport(camWidth, camHeight);
        //set camera so that bottom-left is 0,0
        camera.setXY(camWidth / 2F, camHeight / 2F);
        GL.gl10 = engine.getGraphics().getGL10();


        director = engine.getDirector();
        setupGL();
        engine.getContextManager().register(this);

    }

    public void surfaceChanged(int width, int height) {
        surfaceWidth = width;
        surfaceHeight = height;
        engine.onSurfaceChanged(width, height);
    }

    public AmarenaCamera getCamera() {
        return camera;
    }

    private void setupGL() {

        GL.reset();

        GL.enableBlend();
        GL.blendFunc(BlendFunction.DEFAULT);
        GL.enableTexture2D();

        GL.disableLightning();
        GL.disableDepthTest();
        GL.disableDithering();
        GL.disableMulitsample();
        GL.disableCullFace();
    }

    public void update() {

        engine.onTick(engine.getGraphics().getDeltaTime());

        GL.glViewport(0, 0, surfaceWidth, surfaceHeight);
        GL.clearColor(clearColor);
        GL.clear();

        applyCam();

        director.render();

    }

    public void applyCam() {

        camera.setMatrices();
        camera.updateVisibleRect();
        GL.loadIdentity();

    }

    public void dispose() {

    }


    public void setClearColor(Color clearColor) {
        this.clearColor.set(clearColor);
    }

    public void setClearColor(float r, float g, float b) {
        this.clearColor.set(r, g, b, 1F);
    }

    @Override
    public void onContextLoss() {
        setupGL();
    }
}
